﻿using System;
using System.Collections.ObjectModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QueryDemo
{
	/// <summary>
	/// Dumb control to manage the display of controls used within the editor part.
	/// </summary>
	public partial class QueryDemoEditorControl : UserControl
	{
		/// <summary>
		/// Query type to use.
		/// </summary>
		public QueryType TypeOfQuery
		{
			get
			{
				return (QueryType)Enum.Parse(typeof(QueryType), queryType.SelectedValue);
			}
			set
			{
				queryType.SelectedValue = value.ToString();
			}
		}

		/// <summary>
		/// Title of list for query.
		/// </summary>
		public string ListTitle
		{
			get
			{
				return queryList.SelectedItem.Text;
			}
			set
			{
				if (!String.IsNullOrEmpty(value))
				{
					queryList.SelectedItem.Text = value;
				}
			}
		}

		/// <summary>
		/// Row limit for query.
		/// </summary>
		public int RowLimit
		{
			get
			{
				int retVal;
				if (!Int32.TryParse(rowLimit.Text, out retVal))
				{
					return 10;
				}
				return retVal;
			}
			set {
				rowLimit.Text = (value == 0 ? "10" : value.ToString() );
			}
		}

		/// <summary>
		/// Data source of query types to display.
		/// </summary>
		public object QueryTypeDataSource
		{
			set
			{
				queryType.DataSource = value;
			}
		}

		/// <summary>
		/// Data source of list metadata to display.
		/// </summary>
		public Collection<ListMetadata> ListDataSource
		{
			set
			{
				queryList.Items.Clear();
				foreach (ListMetadata metadata in value)
				{
					queryList.Items.Add(new ListItem(metadata.Title, metadata.GetCombinedProperties()));
				}
			}
		}
	}
}